#include "../libxluploader.h"
#include "sites.h"
#include <assert.h>
#include <iostream>
/*
 A possible response
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <title>imgur: the simple image sharer</title>
 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
 <meta name="keywords" content="image host, image hosting, image sharing, simple image sharer, simple image hoster, simple image sharing, simple image hosting" />
 <meta name="description" content="Imgur is an image hosting service that allows you to easily upload a picture and share it with your friends, post it on Reddit or Digg, or embed it in your blog. You can even resize, crop and rotate it. Share and host your images in seconds!" />
 <meta name="robots" content="FOLLOW,INDEX" />
 <link rel="alternate" type="application/rss+xml" title="Imgur Gallery" href="http://imgur.com/rss" />
 <link rel="stylesheet" href="/include/main-style.css?121009" type="text/css" />
 <link rel="stylesheet" href="/include/share.css?120409" type="text/css" />
 <script type="text/javascript" src="/include/js/jquery.pack.js"></script>
 <!--[if IE]><script type="text/javascript" src="/include/js/curvycorners.pack.js"></script><![endif]-->
 <script type="text/javascript" src="/include/js/jquery.slide.pack.js"></script>
 <script type="text/javascript" src="/include/js/jquery.caption.pack.js?120309"></script>
 <script type="text/javascript" src="/include/js/jquery.validate.pack.js"></script>
 <script type="text/javascript" src="/include/js/jquery.cookie.pack.js"></script>
 <script type="text/javascript"> var ext       = ".png"; var hash      = "Y25av"; var image     = "Y25av.png"; var fromImgur = 1; </script>
 <script type="text/javascript" src="/include/js/share.pack.js?120109"></script>
 </head>
 <body>
 <div id="wrapper"><div id="topbar"><div class="left"><a href="/forum/">forum</a><a href="/tools/">tools</a><a href="/stats/">statistics</a><a href="/gallery/">gallery</a></div></div><div id="secondbar"><div id="imgur-small"><a href="/"><img src="/images/imgur-small.png" alt="the simple image sharer" /></a></div><div id="advertisement"><div id="banner"><script type='text/javascript'> var m3_u = (location.protocol=='https:'?'https://ads.imgur.com/www/delivery/ajs.php':'http://ads.imgur.com/www/delivery/ajs.php'); var m3_r = Math.floor(Math.random()*99999999999); if (!document.MAX_used) document.MAX_used = ','; document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u); document.write ("?zoneid=1"); document.write ('&amp;cb=' + m3_r); if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used); document.write ('&amp;charset=UTF-8'); document.write ("&amp;loc=" + escape(window.location)); if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer)); if (document.context) document.write ("&context=" + escape(document.context)); if (document.mmm_fo) document.write ("&amp;mmm_fo=1"); document.write ("'><\/scr"+"ipt>"); </script></div><div id="your-ad-here"><a href="https://www.projectwonderful.com/advertisehere.php?id=42120&type=5"><img src="/images/your-ad-here.png" alt="Your Ad Here" title="You know you want to..." /></a></div></div><div class="clear"></div></div><div id="view-button">View Full Image</div>  <div id="tag-view" ><table class="main-table"><tr><td><div id="share"><div id="share-top"><div class="share-links textbox" id="post"><a href='http://reddit.com/submit?url=http://imgur.com/Y25av.png' title="Submit to Reddit"><img src="/images/reddit.png" alt="Submit to Reddit" /></a><a href='http://www.digg.com/submit?phase=2&url=http://imgur.com/Y25av.png' title="Submit to Digg"><img src="/images/digg.png" alt="Submit to Digg" /></a><a href='http://www.stumbleupon.com/submit?url=http://imgur.com/Y25av.png' title="Submit to StumbleUpon"><img src="/images/stumble.png" alt="Submit to StumbleUpon" /></a><a href='http://twitter.com/home?status=http://imgur.com/Y25av.png' title="Submit to Twitter"><img src="/images/twitter.png" alt="Submit to Twitter" /></a><a href='http://www.facebook.com/sharer.php?u=http://imgur.com/Y25av.png' title="Submit to Facebook"><img src="/images/facebook.png" alt="Submit to Facebook" /></a></div><div class="textbox sponsored-links"><h4 class="small-margin">Sponsored Link</h4><a href="http://ads.imgur.com/www/delivery/ck.php?oaparams=2__bannerid=8__zoneid=3__cb=43af386b76__oadest=http%3A%2F%2Fweburbanist.com%2F2008%2F10%2F19%2Fghost-town-abandoned-city-examples-images%2F"> 24 Abandoned Cities [Incredible Images!] </a></div><div class="textbox"><h4>Direct Link (email &amp; IM)</h4><input type="text" id="direct" value="http://imgur.com/Y25av.png" readonly="readonly" /></div><div id="html-image" class="textbox"><h4>HTML Image (websites / blogs)</h4><input type="text" id="htmlimage" value='&lt;img src="http://imgur.com/Y25av.png" alt="Hosted by imgur.com" /&gt;' readonly="readonly" /></div><div id="link" class="textbox"><h4>HTML Link (websites / blogs)</h4>
 <input type="text" id="htmllink" value='&lt;a href="http://imgur.com/Y25av.png" title="Hosted by
 imgur.com"&gt;http://imgur.com/Y25av.png&lt;/a&gt;' readonly="readonly" /></div><div id="boards" class="textbox"><h4>Message Boards</h4><input type="text" id="board" value="[IMG]http://imgur.com/Y25av.png[/IMG]" readonly="readonly" /></div><div id="linked-boards" class="textbox"><h4>Linked <span id="label2">Image</span> for Message Boards</h4><input type="text" id="linked-board" value="[URL=http://imgur.com/Y25av.png][IMG]http://imgur.com/Y25av.png[/IMG][/URL]" readonly="readonly" /></div></div><div id="share-bottom"><div id="view-thumbs" class="textbox"><span id="small-image"><img src="/Y25avs.png" width="90" height="90" alt="small thumbnail" /></span><div class="links"><span id="orig"><span class="current-selected">Original Image</span></span><br /><span id="small"><a href="javascript:smallThumb()">Small Thumbnail</a></span><br /><span id="large"><a href="javascript:largeThumb()">Large Thumbnail</a></span><br /></div><div class='images'><div class="voxel"><a href="http://www.voxel.net"><img src="http://i.imgur.com/poweredByVoxel.gif" alt='Powered by Voxel' /></a></div></div><div class="clear"></div></div><div id="data" class="textbox margin-top"><table><tr><td>Views:</td><td class="stat">0</td><td class="datanote">* Not Real-time</td></tr><tr><td>Submitted:</td><td class="stat">1 second ago</td></tr><tr><td>Bandwidth Used:</td><td class="stat">0.00 KB</td></tr></table></div><div id="deletion" class="textbox"><table><tr><td><h4 class="small-margin">Deletion Link:</h4></td><td align="right"><a href="javascript:emailDeletionLinks()">Email It</a></td></tr><tr><td colspan="2"><a href="http://imgur.com/delete/towCMa50Oj">http://imgur.com/delete/towCMa50Oj</a></td></tr></table><div class="delete-info"> This link only shows up once, and this is it. Make sure to save it if you think you might be deleting this image. </div></div><div id="bottom-links" class="margin-top"><div class="left"><a href="javascript:showEmail()">Email This Image</a><br /></div><div class="right"><a href="#" class="bookmark" title="imgur | Y25av">Bookmark This Page</a></div><div id="emailDiv"><form id="email-form" onsubmit="return send_email()" action=""><table class="padded"><tr><td>Your Name:</td><td><input type="text" name="name" id="name" class="required" /></td></tr><tr><td>Their Email:</td><td><input type="text" name="email" id="email" class="required email" /></td></tr><tr><td>Optional Message:</td><td><textarea id="email_message" name="email_message" rows="4" cols="25"></textarea></td></tr><tr><td colspan="2"><input id="submit" type="submit" name="submit" value="Send Email" class="email" /><input type="hidden" value="Y25av" name="id" id="id" /></td></tr></table></form><div id="message"></div><div id="email-success" class="nodisplay">Your email has been sent.</div></div><div id="emailDeleteDiv"><form id="deletion-form" onsubmit="return send_deletionEmail()" action=""><table class="padded"><tr><td>Your Email:</td><td><input type="text" name="email" id="deleteEmail" class="required email" /></td></tr><tr><td colspan="2"><input id="deleteSubmit" type="submit" name="submit" value="Send Email" class="email" /></td></tr></table><input type="hidden" value="towCMa50Oj" name="dHashes" id="dHashes" /><input type="hidden" value="Y25av" name="hashes" id="hashes" /></form><div id="delete-message"></div><div id="delete-email-success" class="nodisplay">Your email has been sent.</div></div><div class="clear"></div></div></div></div><div class="clear"></div><div id="startover"><input type="button" class="new-image" value="" onclick="window.location = '/';" /></div></td><td><div id="preview"><a href="/Y25av.png" id="large-image"><img src="/Y25avl.png" alt="the image" /></a></div></td></tr></table></div><div id="full-view"  class="nodisplay"><table class="main-table"><tr><td id="full-image" ><a href="/Y25av.png"></a></td></tr><tr><td><div id="full-data"><div class="textbox"><table><tr><td>Views:</td><td class="stat">0</td><td rowspan="3" class="voxel"><a href="http://www.voxel.net" target="_blank"><img src="http://i.imgur.com/poweredByVoxel.gif" alt='Powered by Voxel' /></a></td></tr><tr><td>Submitted:</td><td class="stat">1 second ago</td></tr><tr><td>Bandwidth Used:</td><td class="stat">0.00 KB</td></tr></table></div><div class="textbox" id="sponsored-links"><a href="http://ads.imgur.com/www/delivery/ck.php?oaparams=2__bannerid=8__zoneid=3__cb=43af386b76__oadest=http%3A%2F%2Fweburbanist.com%2F2008%2F10%2F19%2Fghost-town-abandoned-city-examples-images%2F"> 24 Abandoned Cities [Incredible Images!] </a></div><div id="sponsored-note" class="sponsored-links">sponsored link</div></div></td></tr></table></div><div class="push">&nbsp;</div></div><div id="footer"><a href="/donate" title="It's a noble cause...">donate</a><a href="/faq" title="have a question?">faq</a><a href="/removalrequest" title="delete an image">request deletion</a><a href="mailto:alan@imgur.com" title="contact me">contact me</a><a href="/legal" title="legal jargon...">legal</a><a href="/advertise" title="advertise with imgur">advertise</a><a href="http://code.google.com/p/imgur-api/" title="the imgur API">api</a></div><script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script><script type="text/javascript"> var pageTracker = _gat._getTracker("UA-6671908-2"); pageTracker._trackPageview(); </script><script type="text/javascript"> _qoptions={ qacct:"p-f8oruOqDFlMeI" }; </script><script type="text/javascript" src="http://edge.quantserve.com/quant.js"></script></body></html> 
*/

std::string CSiteImgur::parseImageUrl (const std::string &response) {
	if (!m_pcreCodePic) {
		return "";
	}

	const int size = 30;
	int ov[size] = {0};
	const char *text = response.c_str();
	int res = pcre_exec(m_pcreCodePic, NULL, text, response.length(), 0, 0, ov, size);
	if (res != 1) {
		return "";
	}

	std::string url = response.substr(ov[0], ov[1] - ov[0]);
	return url;
}

std::string CSiteImgur::getUrl() const {
	return "http://imgur.com/";
}

CSiteImgur::CSiteImgur () {
	m_errPic = NULL;

	const char *pattern_pic = "http://imgur.com/[a-zA-Z0-9]+\\.[a-zA-Z]+";
	int flags = PCRE_BSR_ANYCRLF | PCRE_UTF8 | PCRE_MULTILINE | PCRE_DOTALL;
	m_pcreCodePic = pcre_compile(pattern_pic, flags, &m_errPic, &m_errPicOffset, NULL);
}

CSiteImgur::~CSiteImgur() {
	if (m_pcreCodePic) {
		pcre_free(m_pcreCodePic);
	}
}

TCHAR* CSiteImgur::getSiteName () {
	return _T("Imgur.com");
}

